package com.maizi.socket.message;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.TextMessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.maizi.entity.HuobiResponse;
import com.quant.common.constans.TradeTickerConstants;
import com.quant.common.domain.response.Trade;
import com.quant.common.domain.response.TradeBean;
import com.quant.common.domain.response.TradeResponse;

@SuppressWarnings("rawtypes")
public class HuobiHandleMessage implements IHandleMessage {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@SuppressWarnings("unused")
	private Class clazz;
	
	public HuobiHandleMessage(Class<HuobiResponse> clazz) {
		this.clazz = clazz;
	}
	
	@Override
	public List<TextMessage> subscribes() {
		
		List<TextMessage> subscribes = new ArrayList<TextMessage>();
		
		List<String> symbolList = new ArrayList<>();
		symbolList.add("skm");
		symbolList.add("smt");
		symbolList.add("egt");
		symbolList.add("top");
		symbolList.add("front");
		symbolList.add("lxt");
		symbolList.add("atp");
		symbolList.add("ocn");
		symbolList.add("yee");
		symbolList.add("kan");
		symbolList.add("pvt");
		symbolList.add("dac");
		symbolList.add("cmt");
		symbolList.add("fti");
		symbolList.add("lol");
		symbolList.forEach((s)->{
			subscribes.add(new TextMessage("{\"sub\":\"market."+s+"usdt.trade.detail\",\"id\": \"id1\"}"));
			//subscribes.add(new TextMessage("{\"sub\":\"market."+s+"usdt.bbo\",\"id\": \"id1\"}"));
		});
		// lxt
		
		//subscribes.add(new TextMessage("{\"sub\":\"market.hotusdt.detail\",\"id\": \"id1\"}"));
		
		  // CMT
		//subscribes.add(new TextMessage("{\"sub\":\"market.cmtusdt.detail\",\"id\": \"id1\"}"));
		  // HOT
		//subscribes.add(new TextMessage("{\"sub\":\"market.hotusdt.detail\",\"id\": \"id1\"}"));
		  // FRONT 
		//subscribes.add(new TextMessage("{\"sub\":\"market.frontusdt.detail\",\"id\": \"id1\"}"));
		
		//获取字典的集合

		//ch_trade_detail = ["market.btcusdt.trade.detail","market.ethusdt.trade.detail","market.eosusdt.trade.detail","market.xrpusdt.trade.detail"]

		//需要获取k线的列表
		//ch_trade_kline = ["market.btcusdt.kline.1min","market.ethusdt.kline.1min","market.eosusdt.kline.1min","market.xrpusdt.kline.1min"]

		//需要获取深度的列表
		//ch_trade_deepth = ["market.btcusdt.depth.step0","market.ethusdt.depth.step0","market.eosusdt.depth.step0","market.xrpusdt.depth.step0"]

		//需要获取的市场列表
		//ch_trade_market = ["market.btcusdt.detail","market.ethusdt.detail","market.eosusdt.detail","market.xrpusdt.detail"]
		 
		return subscribes;
	}
	
	@Override
	public void subscribeCallback(String message) {
		
		JSONObject jsonObject = JSONObject.parseObject(message);
		
		// 订阅失败
		if ("error".equals(jsonObject.getString("status"))) {
			log.error("订阅火币WebSockets失败:{}", message);
			
			// 订阅成功时
		} else {
			log.info("订阅火币WebSockets成功:{}", message);
		}
	}
	
	@Override
	public void webSocketError(Throwable exception) {
		log.error("火币WebSockets异常", exception);
	}
	
	@Override
	public void afterConnectionClosed() {
		log.error("火币WebSockets连接关闭");
	}
	
	/**
	 * 接收消息处理
	 */
	@Override
	public void call(String message) {
		TradeResponse<Trade<List<TradeBean>>> rs = JSON.parseObject(message, new TypeReference<TradeResponse<Trade<List<TradeBean>>>>() {});
		if(rs!=null) {
			Trade<List<TradeBean>> trade = rs.getTick();
			List<TradeBean> tradeList = trade.getData();
			if(tradeList!=null&&tradeList.size()>0) {//market.cmtusdt.detail
				if(tradeList.size()>1) {
					tradeList.forEach((bean)->{
						//log.info("成交时间:"+bean.getId()+":成交方向:"+bean.getDirection()+",成交价格:"+bean.getPrice());
					});
				}
				
				//log.info("交易对："+rs.getCh()+",数目="+tradeList.size());
				TradeTickerConstants.tradeDetailMap.put(rs.getCh().replace("market.", "").replace(".trade.detail", ""), tradeList.get(0));
				//log.info("接受消息:{}", tradeList.get(0).getPrice());
			}
			
		}
		
	}
}
